﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MShop.Core.DataAccess;
using MShop.Core.Domain;
using MShop.Core.Service.Context;

namespace MShop.Web.Areas.Admin.Controllers
{
    public class IndexController : SecureController
    {
        private readonly IProductDao _ProductDao;
        private readonly IArticleDao _ArticleDao;
        private readonly IHelpItemDao _HelpItemDao;
        private readonly IContentItemService<Article> _ArticleSerachService;
        private readonly IContentItemService<Product> _ProductSerachService;
        private readonly IContentItemService<HelpItem> _HelpItemSerachService;
        public IndexController(IProductDao productDao,IArticleDao articleDao,IHelpItemDao helpItemDao,
            IContentItemService<Article> articleSerachService,IContentItemService<Product> productSerachService,IContentItemService<HelpItem> helpItemSerachService
            )
        {
            _ProductDao = productDao;
            _ArticleDao = articleDao;
            _HelpItemDao = helpItemDao;
            _ArticleSerachService = articleSerachService;
            _ProductSerachService = productSerachService;
            _HelpItemSerachService = helpItemSerachService;
        }
        public ActionResult ReBuildIndex()
        {
            return View();
        }

        [HttpPost]
        public ActionResult ReBuildIndex(int type)
        {
            switch (type)
            {
                case 1:                   
                    _ArticleDao.GetAll().ToList().ForEach(
                        (x) => {                            
                            _ArticleSerachService.UpdateObject(x);
                        });                       
                    break;
                case 2:
                    _HelpItemDao.GetAll().ToList().ForEach(
                      (x) =>
                      {
                          _HelpItemSerachService.UpdateObject(x);
                      });          
                    break;
                case 3:
                    _ProductDao.GetAll().ToList().ForEach(
                     (x) =>
                     {
                         _ProductSerachService.UpdateObject(x);
                     });       
                    break;
            }
            ViewData["msg"] = "已经成功重建索引";
            return View();
        }

    }
}
